define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'news/buyhouse_learn/index' + location.search,
                    add_url: 'news/buyhouse_learn/add',
                    edit_url: 'news/buyhouse_learn/edit',
                    del_url: 'news/buyhouse_learn/del',
                    multi_url: 'news/buyhouse_learn/multi',
                    import_url: 'news/buyhouse_learn/import',
                    table: 'buyhouse_learn',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'category.name', title: __('Category_id')},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,datetimeFormat:"YYYY-MM-DD"},
                        {field: 'admin.nickname', title: __('Admin.nickname')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            var type = $('#c-type').val()
            // 一进页面就必须初始化一次
            // 普通模式可以不需要
            if(type == 1){
                $('.images-upload').attr('style','display:block;')
                $('.video-frame').attr('style','display:none;')
                $('.outlink-newstype').attr('style','display:none;')
            }else if(type == 2){
                $('.video-frame').attr('style','display:block;')
                $('.images-upload').attr('style','display:none;')
                $('.outlink-newstype').attr('style','display:none;')
            }else if(type == 3){
                $('.outlink-newstype').attr('style','display:block;')
                $('.images-upload').attr('style','display:none;')
                $('.video-frame').attr('style','display:none;')
            }

            $('#c-type').change(function(){
                var type = $(this).val()
                if(type == 0){
                    $('.images-upload').attr('style','display:none;')
                    $('.video-frame').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 1){
                    $('.images-upload').attr('style','display:block;')
                    $('.video-frame').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 2){
                    $('.video-frame').attr('style','display:block;')
                    $('.images-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 3){
                    $('.outlink-newstype').attr('style','display:block;')
                    $('.images-upload').attr('style','display:none;')
                    $('.video-frame').attr('style','display:none;')
                }
            })
        },
        edit: function () {
            Controller.api.bindevent();

            var type = $('#c-type').val()
            // 一进页面就必须初始化一次
            // 普通模式可以不需要
            if(type == 1){
                $('.images-upload').attr('style','display:block;')
                $('.video-frame').attr('style','display:none;')
                $('.outlink-newstype').attr('style','display:none;')
            }else if(type == 2){
                $('.video-frame').attr('style','display:block;')
                $('.images-upload').attr('style','display:none;')
                $('.outlink-newstype').attr('style','display:none;')
            }else if(type == 3){
                $('.outlink-newstype').attr('style','display:block;')
                $('.images-upload').attr('style','display:none;')
                $('.video-frame').attr('style','display:none;')
            }

            $('#c-type').change(function(){
                var type = $(this).val()
                if(type == 0){
                    $('.images-upload').attr('style','display:none;')
                    $('.video-frame').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 1){
                    $('.images-upload').attr('style','display:block;')
                    $('.video-frame').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 2){
                    $('.video-frame').attr('style','display:block;')
                    $('.images-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 3){
                    $('.outlink-newstype').attr('style','display:block;')
                    $('.images-upload').attr('style','display:none;')
                    $('.video-frame').attr('style','display:none;')
                }
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                var input_id = $('#plupload-image').data("input-id") ? $('#plupload-image').data("input-id") : "";
                var preview_id = $('#plupload-image').data("preview-id") ? $('#plupload-image').data("preview-id") : "";

                // console.log(preview_id) // p-image

                if (preview_id) {
                    $(document.body).off("click", "#" + preview_id + " .btn-trash");
                    // 监听事件
                    $("#plupload-image").data("upload-success", function(data, ret) {
                        //这里进行后续操作
                        var imageCount = $('#p-image').children('li').length
                        if(imageCount == 3) {
                            $(this).hide();
                        }
                    });
                    // 移除按钮事件
                    $(document.body).on("click", "#" + preview_id + " .btn-trash", function () {
                        var that = $(this);
                        var path = $("#" + preview_id + " [data-url]");
                        // console.log(path)
                        // console.log($("#plupload-image").data('news-id'))
                        // console.log(path.data("url"))

                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'news/buyhouse_learn/delCover',
                                    data: {
                                        newsid: $("#plupload-image").data('news-id'),
                                        path: path.data("url")
                                    }
                                }
                                Fast.api.ajax(options, function (d) {
                                    // console.log(123)
                                    if (input_id) {
                                        $("#" + input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-image").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }

                // 删除图片集
                // 仿上面重新写一次图片的删除提示
                var images_input_id = $('#plupload-images').data("input-id") ? $('#plupload-images').data("input-id") : "";
                var images_preview_id = $('#plupload-images').data("preview-id") ? $('#plupload-images').data("preview-id") : "";

                if (images_preview_id) {
                    $(document.body).off("click", "#" + images_preview_id + " .btn-trash");

                    // 移除按钮事件
                    $(document.body).on("click", "#" + images_preview_id + " .btn-trash", function () {
                        var that = $(this);
                        var path = $("#" + images_preview_id + " [data-url]");
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'news/buyhouse_learn/delImages',
                                    data: {
                                        newsid: $("#plupload-image").data('news-id'),
                                        path: path.data("url")
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (images_input_id) {
                                        $("#" + images_input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-image").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }
            }
        }
    };
    return Controller;
});
